package zh.dummy.netty.server.util;

import java.util.HashMap;
import java.util.Map;

import com.alibaba.fastjson.JSONObject;

import zh.dummy.netty.core.protocol.ErrorType;
import zh.dummy.netty.core.protocol.Header;
import zh.dummy.netty.core.protocol.MessageType;
import zh.dummy.netty.core.protocol.NettyMessage;

public class NettyMessageBuilder {
	
	private final static NettyMessage heatBeatResp;
	
	static{
		NettyMessage message = new NettyMessage();
		Header header = new Header();
		header.setType(MessageType.HEARTBEAT_RESP.value());
		message.setHeader(header);
		heatBeatResp = message;
	}
	
	public static NettyMessage buildWorkflowMessage(Map<String, Object> message) {
		Header header = new Header();
		header.setType(MessageType.WORK_FLOW.getValue());
		return new NettyMessage(header,message);
	}
	
	public static NettyMessage buildHeatBeatResp(){
		return heatBeatResp;
	}

	public static NettyMessage buildAuthResp(String id,ErrorType errorType) {
		NettyMessage message = new NettyMessage();
		Header header = new Header();
		header.setType(MessageType.LOGIN_RESP.value());
		Map<String,Object> attchment = new HashMap<String,Object>();
		attchment.put("id", id);
		header.setAttchment(attchment);
		message.setHeader(header);
		JSONObject body = new JSONObject();
		body.put("errorCode", errorType.getValue());
		body.put("errorMsg", errorType.getName());
		message.setBody(body);
		return message;
	}
}
